package gameDisplayer;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import gameDisplayer.Canvas;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import gameDisplayer.*;
import gameManager.GameManager;
import gameManager.Interactable;

public class GameDisplayer {

    private Game myGame;
    private JFrame myFrame;
    private GameManager myGameManager;
    private String myLevel;
    private List<Interactable> myInteractableCharacters;
    private Canvas myCanvas;
    
    
    private static final Dimension SIZE = new Dimension(950, 500);
    
    public GameDisplayer (GameManager m) {
        myGameManager = m;
        myFrame = new JFrame();
        myInteractableCharacters=m.getInteractableObjects();
        myGame = new Game(myInteractableCharacters);
        
    }
    
    /**
     * Initializes the Jframe
     */
   
    private JComboBox c = new JComboBox();
    public void paintGameObjects(){
        
        //myGame.paintComponent((Graphics2D)pen, SIZE);
        updateDisplay();
    }
    /**
     * Updates and repaints the display
     */
    public void updateDisplay () {
        JComponent canvas = getGame(SIZE);
        myFrame.getContentPane().removeAll();
        myFrame.getContentPane().add(canvas);
         myFrame.repaint();
        myFrame.setTitle("Game one");
        myFrame.setVisible(true);
    }

    /**
     * Initializes the Jframe
     */
    public void initializeFrame () {
         myFrame.setSize(SIZE);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setMinimumSize(SIZE);
        myFrame.setLocationRelativeTo(null);
        myFrame.setVisible(true);
        myFrame.setFocusable(true);
        myFrame.addKeyListener(new AL());
    }
    
    
    
    public JComponent getGame (Dimension dim) {
        Canvas canvas = new Canvas(dim, myGame);
        return canvas;
    }

    
    
    private JMenu createLevelMenu () {
        JMenu file = new JMenu("Levels");
        return file;
    }
    
    
    public void displayStartupMenu() {
        List<String> levelList = myGameManager.getLevels();
        for (int i = 0; i < levelList.size(); i++)
            c.addItem(levelList.get(i));
          
          c.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myLevel="" + ((JComboBox) e.getSource()).getSelectedItem();
              System.out.println("" + ((JComboBox) e.getSource()).getSelectedItem());
              myGameManager.setLevel(myLevel);
              initializeFrame();
              myGame.setmyBackGroundImage(myGameManager.getBackgroundImage());
              updateDisplay();
              //drawBackground(myGameManager.getBackgroundImage());
              myGameManager.initializePlayerCharacter();
            }
          });
          JFrame myFrame=new JFrame();
          myFrame.add(c);
          myFrame.setVisible(true);
          myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          myFrame.setSize(300, 300);
    }

public void drawBackground(Graphics2D pen,Image myImage){
    
    myGame.drawBackground(pen,myImage);
}

private class AL extends KeyAdapter{
    
    public void keyReleased(KeyEvent e){
        myGameManager.keyReleased(e);
    }
    public void keyPressed(KeyEvent e){
        myGameManager.keyPressed(e);
    }
}    
    
}
